<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html, body {margin: 0;height: 100%;}
    #rec {width: 100%;height: 100%;}
  </style>
</head>
<body>
  <div id="rec">
    <canvas id="canvas"></canvas>
  </div>
  <script>
    /** @type {HTMLCanvasElement} */
    const rec = document.querySelector('#rec');
    const canvas = document.querySelector('#canvas');
    const { clientWidth, clientHeight } = rec;
    canvas.width = clientWidth;
    canvas.height = clientHeight;
    const ctx = canvas.getContext('2d');

    // 地板和瓷砖
    const floor = new Image();
    floor.src = './images/floor.jpg';
    const title = new Image();
    title.src = './images/tile.jpg';

    // 当所有图片都加载成功后再绘图
    Promise.all([imgPro(floor), imgPro(title)]).then(() => {
      // 绘图方法
      draw();
    })

    function imgPro(img) {
      return new Promise(resolve => {
        img.onload = function () {
          resolve(img);
        }
      })
    }

    function draw() {
      // 纹理对象
      const ptf = ctx.createPattern(floor, 'repeat');
      const ptt = ctx.createPattern(title, 'repeat');
      // 赋值
      ctx.fillStyle = ptf;
      ctx.strokeStyle = ptt;
      // 描边宽度
      ctx.lineWidth=60;
      //绘图
      ctx.fillRect(0,0,clientWidth,clientHeight);
      ctx.strokeRect(0,0,clientWidth,clientHeight);
    }
  </script>
</body>
</html>